package com.primaryedu.ui.app.parentsch;

import com.bumptech.glide.Glide;
import com.primaryedu.R;
import com.primaryedu.bean.VotelistBean;
import com.primaryedu.data.Contants;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 投票管理
 *
 * @author jun.wang
 * @description
 * @data 2018年1月8日
 */
public class VoteAdapter extends BaseAdapter {

    private List<VotelistBean.DataBean.ListBean> list = new ArrayList<>();
    private LayoutInflater inflater;
    @SuppressWarnings("unused")
    private Context context;
    private ViewHolder viewHolder;

    public VoteAdapter(Activity context, List<VotelistBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public void updateVoteView(List<VotelistBean.DataBean.ListBean> nowList) {
        this.list = nowList;
        this.notifyDataSetChanged();//强制动态刷新数据进而调用getView方法
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder = null;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_vote, null);
            viewHolder.image = (ImageView) convertView.findViewById(R.id.ll_vote_image);
            viewHolder.endtime = (TextView) convertView.findViewById(R.id.tv_endtime_vote);
            viewHolder.name = (TextView) convertView.findViewById(R.id.tv_name_vote);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (VoteAdapter.ViewHolder) convertView.getTag();
        }
        VotelistBean.DataBean.ListBean vb = list.get(position);
        Log.i("yy", "getView: " + vb.getEndTime());
        viewHolder.endtime.setText("投票截止时间: " + vb.getEndTime());
        viewHolder.name.setText(vb.getName());

        Glide.with(convertView.getContext())
                .load(Contants.course_api + vb.getPic())
                .placeholder(R.drawable.test_a)
                .error(R.drawable.test_a)
                .centerCrop()
                .crossFade()
                .into(viewHolder.image);
        return convertView;
    }

    class ViewHolder {
        ImageView image;
        TextView endtime;
        TextView name;
        TextView tv_name_time;
    }
}
